package com.ruoyi.xiaoshou.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.security.annotation.InnerAuth;
import com.ruoyi.xiaoshou.domain.SaleBack;
import com.ruoyi.xiaoshou.domain.StockOut;
import com.ruoyi.xiaoshou.domain.StockOutDetail;
import com.ruoyi.xiaoshou.service.IStockOutDetailService;
import com.ruoyi.xiaoshou.service.IStockOutService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.xiaoshou.domain.SaleBackRequest;
import com.ruoyi.xiaoshou.service.ISaleBackRequestService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 销售退货申请Controller
 * 
 * @author lmj
 * @date 2024-10-25
 */
@RestController
@RequestMapping("/saleBackRequest")
@Slf4j
public class SaleBackRequestController extends BaseController
{
    @Autowired
    private ISaleBackRequestService saleBackRequestService;
    @Autowired
    private IStockOutService stockOutService;
    @Autowired
    private IStockOutDetailService stockOutDetailService;

    /**
     * 查询销售退货申请列表
     */
    @RequiresPermissions("xiaoshou:saleBackRequest:list")
    @GetMapping("/list")
    public TableDataInfo list(SaleBackRequest saleBackRequest)
    {
        startPage();
        List<SaleBackRequest> list = saleBackRequestService.selectSaleBackRequestList(saleBackRequest);
        return getDataTable(list);
    }

    /**
     * 导出销售退货申请列表
     */
    @RequiresPermissions("xiaoshou:saleBackRequest:export")
    @Log(title = "销售退货申请", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SaleBackRequest saleBackRequest)
    {
        List<SaleBackRequest> list = saleBackRequestService.selectSaleBackRequestList(saleBackRequest);
        ExcelUtil<SaleBackRequest> util = new ExcelUtil<SaleBackRequest>(SaleBackRequest.class);
        util.exportExcel(response, list, "销售退货申请数据");
    }

    /**
     * 获取销售退货申请详细信息
     */
    @RequiresPermissions("xiaoshou:saleBackRequest:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return success(saleBackRequestService.selectSaleBackRequestById(id));
    }

    /**
     * 新增销售退货申请
     */
    @RequiresPermissions("xiaoshou:saleBackRequest:add")
    @Log(title = "销售退货申请", businessType = BusinessType.INSERT)
    @RequestMapping("/addSaleBackRequest")
    public AjaxResult addSaleBackRequest(@RequestBody SaleBackRequest saleBackRequest)
    {
        log.info("SaleBackRequestController addSaleBackRequest params:{}",saleBackRequest);
        return saleBackRequestService.insertSaleBackRequest(saleBackRequest);
    }
    @RequiresPermissions("xiaoshou:saleBackRequest:add")
    @RequestMapping("getStockOutList")
    public TableDataInfo getStockOutList(StockOut stockOut)
    {
        startPage();
        List<StockOut> stockOutList = stockOutService.getStockOutList(stockOut);
        return getDataTable(stockOutList);
    }

    @InnerAuth
    @GetMapping("getSaleBackRequestByCode/{code}")
    public R<SaleBackRequest> getSaleBackRequestByCode(@PathVariable("code")String code){
        log.info("SaleBackController getSaleBackRequestByCode params:{}",code);
        SaleBackRequest saleBackRequest = new SaleBackRequest();
        saleBackRequest.setSaleBackRequestCode(code);
        List<SaleBackRequest> saleBackRequests = saleBackRequestService.selectSaleBackRequestList(saleBackRequest);
        if (saleBackRequests.size()==1){
            return R.ok(saleBackRequests.get(0));
        }
        return R.fail();
    }

    @RequiresPermissions("xiaoshou:saleBackRequest:add")
    @RequestMapping("getStockOutByCode")
    public AjaxResult getStockOutByCode(StockOut stockOut)
    {
        return stockOutService.getStockOutByCode(stockOut);
    }
    @PostMapping("getSaleBackRequestListBySaleOutCode")
    public R<List<SaleBackRequest>> getSaleBackRequestListBySaleOutCode(@RequestBody String saleOutCode){
        log.info("SaleBackRequestController getSaleBackRequestListBySaleOutCode params{}",saleOutCode);
        SaleBackRequest saleBackRequest = new SaleBackRequest();
        saleBackRequest.setSaleOutCode(saleOutCode);
        List<SaleBackRequest> saleBackRequests = saleBackRequestService.selectSaleBackRequestList(saleBackRequest);
        return R.ok(saleBackRequests);
    }

    @RequiresPermissions("xiaoshou:saleBackRequest:add")
    @RequestMapping("getStockOutDetailList")
    public TableDataInfo getStockOutDetailList(StockOutDetail stockOutDetail)
    {
        startPage();
        List<StockOutDetail> stockOutDetailList = stockOutDetailService.getStockOutDetailList(stockOutDetail);
        return getDataTable(stockOutDetailList);
    }

    /**
     * 修改销售退货申请
     */
    @RequiresPermissions("xiaoshou:saleBackRequest:edit")
    @Log(title = "销售退货申请", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SaleBackRequest saleBackRequest)
    {
        return saleBackRequestService.updateSaleBackRequest(saleBackRequest);
    }

    /**
     * 审核
     *
     */
    @RequiresPermissions("xiaoshou:saleBackRequest:edit")
    @Log(title = "审核销售退货申请", businessType = BusinessType.UPDATE)
    @RequestMapping("checkSaleRequest/{ids}/{status}/{userId}")
    public AjaxResult checkSaleRequest(@PathVariable("ids") Integer[] ids,@PathVariable("status") Integer status,@PathVariable("userId") String userId)
    {
        log.info("SaleBackRequestController checkSaleRequest params:ids:{},status:{},userId:{}",ids,status,userId);
        return saleBackRequestService.checkSaleBackRequest(ids,status,userId);
    }
    /**
     * 删除销售退货申请
     */
    @RequiresPermissions("xiaoshou:saleBackRequest:remove")
    @Log(title = "销售退货申请", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(saleBackRequestService.deleteSaleBackRequestByIds(ids));
    }
    @PostMapping("updSaleBackRequestByCode")
    public R<String> updSaleBackRequestByCode(@RequestBody SaleBackRequest saleBackRequest){
        log.info("SaleBackRequestController updSaleBackByCode params:{}",saleBackRequest);
        return R.ok(saleBackRequestService.updSaleBackRequestByCode(saleBackRequest));
    }
}
